﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JsonComaprer.NET
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test_Null_JsonStrings_As_True();

            //Test_Null_JsonString_As_False();

            //Test_Not_Null_JsonStrings_As_True();

            //Test_Not_Null_JsonStrings_As_False();

            //Test_Not_Null_JsonStrings_As_False1();


            //Test__CompareJsonString_Passed();

            //Test__CompareJsonString_Passed1();

            //Test__CompareJsonString2_Passed2();

            //Test__CompareJsonString2_Passed3();

            Console.ReadKey();
        }

        static void Test_Null_JsonStrings_As_True()
        {
            String str1 = "";
            String str2 = "";
            List<CompareResult> tmp = new JsonComparer(new JsonComparerSetting()).IsTheSameJsonString(str1, str2);

            if (tmp == null || tmp.Count == 0)
            {
                Console.WriteLine("True");
            }
            else
            {
                foreach (var item in tmp)
                {
                    Console.WriteLine(String.Format("key={0}, value1={1}, value2={2}", item.Key, item.FirstValue, item.SecondValue));
                }
            }
        }

        static void Test_Null_JsonString_As_False()
        {
            String str1 = "{a:1}";
            String str2 = "";
            List<CompareResult> tmp = new JsonComparer(new JsonComparerSetting()).IsTheSameJsonString(str1, str2);

            if (tmp == null || tmp.Count == 0)
            {
                Console.WriteLine("There is no difference");
            }
            else
            {
                foreach (var item in tmp)
                {
                    Console.WriteLine(String.Format("key={0}, value1={1}, value2={2}", item.Key, item.FirstValue, item.SecondValue));
                }
            }
        }

        static void Test_Not_Null_JsonStrings_As_True()
        {
            String str1 = "{a:1}";
            String str2 = "{a:1}";
            List<CompareResult> tmp = new JsonComparer(new JsonComparerSetting()).IsTheSameJsonString(str1, str2);

            if (tmp == null || tmp.Count == 0)
            {
                Console.WriteLine("true");
            }
            else
            {
                foreach (var item in tmp)
                {
                    Console.WriteLine(String.Format("key={0}, value1={1}, value2={2}", item.Key, item.FirstValue, item.SecondValue));
                }
            }
        }

        static void Test_Not_Null_JsonStrings_As_False()
        {
            String str1 = "{	\"name\": \"GetUserVoteTicketMain\",	\"resourceId\": \"171544536\",	\"callbackUrl\": null,	\"master\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 14,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"2016-05-10T09:43:07\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594746,		\"timeConsumption\": 37,		\"isOk\": true	},	\"slave\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 26,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"0001-01-01T00:00:00\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594785,		\"timeConsumption\": 617,		\"isOk\": true	},	\"uuid\": \"QD101-83ebb40cce764147f5a2f43099489c8e4f4665\",	\"adapteeConsumption\": 37,	\"adaptorSide\": true,	\"networkLatency\": 35}";
            String str2 = "{	\"name\": \"GetUserVoteTicketMain\",	\"resourceId\": \"171544536\",	\"callbackUrl\": null,	\"master\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 14,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"2016-05-10T09:43:07\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594746,		\"timeConsumption\": 37,		\"isOk\": true	},	\"slave\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 26,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"0001-01-01T00:00:00\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594785,		\"timeConsumption\": 617,		\"isOk\": true	},	\"uuid\": \"QD101-83ebb40cce712364147f5a2f43099489c8e4f4665\",	\"adapteeConsumption\": 37,	\"adaptorSide\": true,	\"networkLatency\": 35}";
            List<CompareResult> tmp = new JsonComparer(new JsonComparerSetting()).IsTheSameJsonString(str1, str2);

            if (tmp == null || tmp.Count == 0)
            {
                Console.WriteLine("False");
            }
            else
            {
                foreach (var item in tmp)
                {
                    Console.WriteLine(String.Format("key={0}, value1={1}, value2={2}", item.Key, item.FirstValue, item.SecondValue));
                }
            }
        }

        static void Test_Not_Null_JsonStrings_As_False1()
        {
            String str1 = "{	\"name\": \"GetUserVoteTicketMain\",	\"resourceId\": \"171544536\",	\"callbackUrl\": null,	\"master\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 14,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"2016-05-10T09:43:07\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594746,		\"timeConsumption\": 37,		\"isOk\": true	},	\"slave\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 26,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"0001-01-01T00:00:00\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594785,		\"timeConsumption\": 617,		\"isOk\": true	},	\"uuid\": \"QD101-83ebb40cce764147f5a2f43099489c8e4f4665\",	\"adapteeConsumption\": 37,	\"adaptorSide\": true,	\"networkLatency\": 35}";
            String str2 = "{	\"name\": \"GetUserVoteTicketMain\",	\"resourceId\": \"171544536\",	                    	\"master\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 14,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"2016-05-10T09:43:07\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594746,		\"timeConsumption\": 37,		\"isOk\": true	},	\"slave\": {		\"req\": {			\"Item1\": 171544536,			\"Item2\": \"1000\",			\"Item3\": \"203.110.179.201\",			\"Item4\": 0		},		\"resp\": {			\"Code\": 0,			\"SubCode\": 0,			\"DataCount\": 0,			\"Message\": \"获取推荐票信息成功!\",			\"Error\": \"\",			\"Response\": {				\"UserId\": 171544536,				\"MaxRecomTicket\": 10,				\"TodayUsedRecomTicket\": 0,				\"TodayMMUsedRecomTicket\": 0,				\"TodayWXUsedRecomTicket\": 0,				\"TotalRecomCount\": 26,				\"TotalMMRecomCount\": 0,				\"TotalWXRecomCount\": 0,				\"LastUpdateTime\": \"0001-01-01T00:00:00\",				\"TicketAvailableMain\": 10,				\"TicketAvailableWX\": 10,				\"TicketAvallableMM\": 10			}		},		\"startTime\": 1462844594785,		\"timeConsumption\": 617,		\"isOk\": true	},	\"uuid\": \"QD101-83ebb40cce712364147f5a2f43099489c8e4f4665\",	\"adapteeConsumption\": 37,	\"adaptorSide\": true,	\"networkLatency\": 35}";
            List<CompareResult> tmp = new JsonComparer(new JsonComparerSetting()).IsTheSameJsonString(str1, str2);

            if (tmp == null || tmp.Count == 0)
            {
                Console.WriteLine("False");
            }
            else
            {
                foreach (var item in tmp)
                {
                    Console.WriteLine(String.Format("key={0}, value1={1}, value2={2}", item.Key, item.FirstValue, item.SecondValue));
                }
            }
        }



        
        static void Test__CompareJsonString_Passed()
        {
            JsonComparerSetting setting = new JsonComparerSetting();
            setting.EquesMappings = new Dictionary<string, EqualsMappingSetting>();
            setting.EquesMappings.Add("NoteName", new EqualsMappingSetting()
            {
                LeftValues = new List<string>() { "Aaa", "ACa", "ABa" },
                RightValues = new List<string>() { "aaa" }
            });
            JsonComparer json = new JsonComparer(setting);
            List<CompareResult> tmp = json._CompareJsonString("AAA", "aaa", "NoteName");
            Console.WriteLine(tmp == null || tmp.Count==0? "Passed" : "Not passed");
        }

        static void Test__CompareJsonString_Passed1()
        {
            JsonComparerSetting setting = new JsonComparerSetting();
            
            setting.EquesMappings = new Dictionary<string, EqualsMappingSetting>();
            setting.EquesMappings.Add("NoteName", new EqualsMappingSetting()
            {
                LeftValues = new List<string>() { "Aaa", "ACa", "ABa" },
                RightValues = new List<string>() { "aaa" }
            });
            JsonComparer json = new JsonComparer(setting);
            List<CompareResult> tmp = json._CompareJsonString("AAA", "aAA", "NoteName");
            Console.WriteLine(tmp == null || tmp.Count == 0 ? "Passed" : "Not passed");
        }

        static void Test__CompareJsonString2_Passed2()
        {
            JsonComparerSetting setting = new JsonComparerSetting();
            
            setting.EquesMappings = new Dictionary<string, EqualsMappingSetting>();
            setting.EquesMappings.Add("NoteName", new EqualsMappingSetting()
            {
                LeftValues = new List<string>() { "Aaa", "ACa", "ABa" },
                RightValues = new List<string>() { "aaa" }
            });
            JsonComparer json = new JsonComparer(setting);
            List<CompareResult> tmp = json._CompareJsonString("ACa", "aaa", "NoteName");
            Console.WriteLine(tmp == null || tmp.Count == 0 ? "Passed" : "Not passed");
        }

        static void Test__CompareJsonString2_Passed3()
        {
            JsonComparerSetting setting = new JsonComparerSetting();
            
            setting.EquesMappings = new Dictionary<string, EqualsMappingSetting>();
            setting.EquesMappings.Add("NoteName", new EqualsMappingSetting()
            {
                LeftValues = new List<string>() { "Aaa", "ACa", "ABa" },
                RightValues = new List<string>() { "aaa","CCCC" }
            });
            JsonComparer json = new JsonComparer(setting);
            List<CompareResult> tmp = json._CompareJsonString("ACa", "cccc", "NoteName");
            Console.WriteLine(tmp == null || tmp.Count == 0 ? "Passed" : "Not passed");
        }
    }



}